import pymysql
from logger_config import logging

logger = logging.getLogger(__name__)


def create_db_connection(config):
    """
    Create and return a database connection and cursor.

    Parameters:
        config (dict): Database configuration dictionary.

    Returns:
        tuple: (cursor, connection), or (None, None) if connection fails.
    """
    try:
        connection = pymysql.connect(
            host=config['db']['host'],
            port=config['db']['port'],
            user=config['db']['user'],
            password=config['db']['password'],
            db=config['db']['database'],
            charset=config['db']['charset'],
            cursorclass=pymysql.cursors.DictCursor
        )
        return connection.cursor(), connection
    except pymysql.MySQLError as e:
        logger.error(f"Database connection failed: {e}")
        return None, None